package com.fyzk.activity.lottery.jc.football.newJz;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.AbsListView;

import com.fyzk.activity.app.ActivityTaskManager;
import com.fyzk.activity.lottery.jc.football.pop.JCZQFilterGameQueuePop;
import com.fyzk.activity.lottery.jc.football.pop.JCZQSelectPlayMethodPop;
import com.fyzk.activity.lottery.shuzicai.InfoDetailActivity;
import com.fyzk.activity.myview.NoSwipeViewPager;
import com.fyzk.activity.prize.JZPrizeActivity;
import com.fyzk.activity.user.LoginActivity;
import com.fyzk.activity.user.touzhu.CommonBetActivity;
import com.fyzk.base.IBaseActivity;
import com.fyzk.data.AppLotteryInfos;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.interfaces.OnBetCheckedListener;
import com.fyzk.utils.HelpUrlConstants;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.utils.LogUtil;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.view.DownMenuPopupwindow;
import com.fyzk.view.FootTitleView;
import com.fyzk.view.ViewCallBack;
import com.tyjc.lottery.R;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

@ContentView(R.layout.ac_football_fragment)
public class FootballFragmentActivityI extends IBaseActivity implements JCZQFilterGameQueuePop
        .OnFilterListener, OnBetCheckedListener, DownMenuPopupwindow.DownMenuPopupItemListener {

    @ViewInject(R.id.jczq_toptitle_view)
    public FootTitleView topTitleView;

    @ViewInject(R.id.shop_detail_pager)
    private NoSwipeViewPager shop_detail_pager;

    private JCZQSelectPlayMethodPop top_popup;
    private JCZQFilterGameQueuePop filter_popup;

    FragmentPagerAdapter fAdapter;

    JCZQBaseFragment[] fList;

    private JCZQBaseFragment curFragment = null;// 当前fragment

    // 过关投注 胜平负Fragment，半全场Fragment，比分Fragment，总进球Fragment，混合投注，
    private JCZQBaseFragment spfFragment, bqcFragment, bfFragment, zjqFragment, hgFragment;
    // 单关投注 胜平负Fragment，半全场Fragment，比分Fragment，总进球Fragment
    private JCZQBaseFragment spfdFragment, bqcdFragment, bfdFragment, zjqdFragment;

    private final int SPF = 0;
    private final int SPFD = 1;
    private final int BQC = 2;
    private final int BQCD = 3;
    private final int BF = 4;
    private final int BFD = 5;
    private final int ZJQ = 6;
    private final int ZJQD = 7;
    private final int HG = 8;

    private DownMenuPopupwindow downMenu;

    // 是否从首页竞足单关进入
    private boolean isDanGuanInto = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isDanGuanInto = getIntent().getBooleanExtra("isDan", false);
        topTitleView.setRightVisible(View.VISIBLE);
        topTitleView.setJianTouVisible(View.VISIBLE);
        topTitleView.setRight2Visible(View.VISIBLE);
        topTitleView.setOnTitleClickListener(this);
        topTitleView.setRightImg(R.drawable.ic_question);
        top_popup = new JCZQSelectPlayMethodPop(this,
                AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT, isDanGuanInto);
        filter_popup = new JCZQFilterGameQueuePop(this,
                AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
        filter_popup.setOnFilterListener(this);

        spfFragment = new JCZQ_SPF_Fragment();
        spfdFragment = new JCZQ_SPF_Fragment();
        spfdFragment.setDanguan(true);

        bqcFragment = new JCZQ_BQC_Fragment();
        bqcdFragment = new JCZQ_BQC_Fragment();
        bqcdFragment.setDanguan(true);

        bfFragment = new JCZQ_BF_Fragment();
        bfdFragment = new JCZQ_BF_Fragment();
        bfdFragment.setDanguan(true);

        zjqFragment = new JCZQ_ZJQ_Fragment();
        zjqdFragment = new JCZQ_ZJQ_Fragment();
        zjqdFragment.setDanguan(true);

        hgFragment = new JCZQ_HG_Fragment();

        fList = new JCZQBaseFragment[9];
        fList[0] = spfFragment;
        fList[1] = spfdFragment;
        fList[2] = bqcFragment;
        fList[3] = bqcdFragment;
        fList[4] = bfFragment;
        fList[5] = bfdFragment;
        fList[6] = zjqFragment;
        fList[7] = zjqdFragment;
        fList[8] = hgFragment;

        fAdapter = new JCZQAdapter(getSupportFragmentManager());
        shop_detail_pager.setAdapter(fAdapter);
        shop_detail_pager.setOffscreenPageLimit(fList.length - 1);

        if (isDanGuanInto) {
            curFragment = spfdFragment;
            topTitleView.setCenterText("胜平负(单关)");
            shop_detail_pager.setCurrentItem(SPFD);
        } else {
            curFragment = hgFragment;
            topTitleView.setCenterText("混合投注");
            shop_detail_pager.setCurrentItem(HG);
        }

        shop_detail_pager.setOnTouchListener(null);
        shop_detail_pager.addOnPageChangeListener(pageChangeListener);

        top_popup.setOnWFCheckChangedListener(wfChangedListener);
        downMenu = new DownMenuPopupwindow(this,
                AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT,
                getMenuItems());
        downMenu.setDownMenuPopupOnItemListener(this);
    }

    protected String[] getMenuItems() {
        return new String[]{"开奖公告", "玩法介绍", "投注查询"};
    }

    class JCZQAdapter extends FragmentPagerAdapter {

        public JCZQAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fList[position];
        }

        @Override
        public int getCount() {
            return fList.length;
        }
    }

    OnPageChangeListener pageChangeListener = new OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            if (curFragment == null) {
                switch (position) {
                    case SPF:
                        spfFragment = new JCZQ_SPF_Fragment();
                        break;
                    case SPFD:
                        spfdFragment = new JCZQ_SPF_Fragment();
                        spfdFragment.setDanguan(true);
                        break;
                    case BQC:
                        bqcFragment = new JCZQ_BQC_Fragment();
                        break;
                    case BQCD:
                        bqcdFragment = new JCZQ_BQC_Fragment();
                        bqcdFragment.setDanguan(true);
                        break;
                    case BF:
                        bfFragment = new JCZQ_BF_Fragment();
                        break;
                    case BFD:
                        bfdFragment = new JCZQ_BF_Fragment();
                        bfdFragment.setDanguan(true);
                        break;
                    case ZJQ:
                        zjqFragment = new JCZQ_ZJQ_Fragment();
                        break;
                    case ZJQD:
                        zjqdFragment = new JCZQ_ZJQ_Fragment();
                        zjqdFragment.setDanguan(true);
                        break;
                    case HG:
                        hgFragment = new JCZQ_HG_Fragment();
                        break;
                    default:
                        break;
                }
            }
            curFragment.clearSelect();
            curFragment.reTry();
        }

        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }

    };

    //    @Override
    public void onBetChecked() {
//        changCi = curFragment.curFragment.countChangCi();
//        if (curFragment.curFragment.isDanguan) {
//            minChang = 1;
//        } else {
//            minChang = 2;
//        }
//        setBtmView();
    }

    JCZQSelectPlayMethodPop.OnWFCheckChangedListener wfChangedListener = new
            JCZQSelectPlayMethodPop.OnWFCheckChangedListener() {

                @Override
                public void onWFCheckChange(int id, boolean isChecked) {
                    if (!isChecked) {
                        return;
                    }
                    switch (id) {
                        case R.id.cb_selectplaymethod_spf_zq:
                            topTitleView.setCenterText("胜平负(过关)");
                            curFragment = spfFragment;
                            shop_detail_pager.setCurrentItem(SPF, false);
                            break;
                        case R.id.cb_selectplaymethod_d_spf_zq:
                            topTitleView.setCenterText("胜平负(单关)");
                            curFragment = spfdFragment;
                            shop_detail_pager.setCurrentItem(SPFD, false);
                            break;
                        case R.id.cb_selectplaymethod_bqc_zq:
                            topTitleView.setCenterText("半全场(过关)");
                            curFragment = bqcFragment;
                            shop_detail_pager.setCurrentItem(BQC, false);
                            break;
                        case R.id.cb_selectplaymethod_d_bqc_zq:
                            topTitleView.setCenterText("半全场(单关)");
                            curFragment = bqcdFragment;
                            shop_detail_pager.setCurrentItem(BQCD, false);
                            break;
                        case R.id.cb_selectplaymethod_bf_zq:
                            topTitleView.setCenterText("比分(过关)");
                            curFragment = bfFragment;
                            shop_detail_pager.setCurrentItem(BF, false);
                            break;
                        case R.id.cb_selectplaymethod_d_bf_zq:
                            topTitleView.setCenterText("比分(单关)");
                            curFragment = bfdFragment;
                            shop_detail_pager.setCurrentItem(BFD, false);
                            break;
                        case R.id.cb_selectplaymethod_zjq_zq:
                            topTitleView.setCenterText("总进球(过关)");
                            curFragment = zjqFragment;
                            shop_detail_pager.setCurrentItem(ZJQ, false);
                            break;
                        case R.id.cb_selectplaymethod_d_zjq_zq:
                            topTitleView.setCenterText("总进球(单关)");
                            curFragment = zjqdFragment;
                            shop_detail_pager.setCurrentItem(ZJQD, false);
                            break;
                        case R.id.cb_selectplaymethod_hg_zq:
                            topTitleView.setCenterText("混合投注");
                            curFragment = hgFragment;
                            shop_detail_pager.setCurrentItem(HG, false);
                            break;
                    }
                    top_popup.dismiss();
                    onBetChecked();//切换玩法刷新场次信息
                }
            };

    @Override
    public void onCenterClick() {
        top_popup.showAsDropDown(topTitleView);
    }

    @Override
    public void onRightClick() {
        toHelp();
    }

    @Override
    public void onRight2Click() {
        filter_popup.setGames(curFragment.getTeams(),
                curFragment.teams_sel);
        filter_popup.showAsDropDown(topTitleView);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        curFragment.onActivityResult(requestCode, resultCode, data);
    }

    protected FootTitleView getTopView() {
        return topTitleView;
    }

    /**
     * 进入玩法介绍
     */
    public void toHelp() {
        if (getTopView() != null) {
            downMenu.showPopupWindow(getTopView());
        } else {
            Intent intent = new Intent(getContext(), InfoDetailActivity.class);
            intent.putExtra(IntentParamUtils.TITLE, getHelpTitle());
            intent.putExtra(IntentParamUtils.URL, getHelpUrl());
            startActivity(intent);
        }
    }

    @Override
    public void onMenuItem(int position) {
        LogUtil.d("" + getMenuItems()[position]);
        Intent intent = null;
        switch (position) {
            case 0:
                // 开奖公告
                intent = new Intent();
                intent.setClass(getContext(), JZPrizeActivity.class);
                intent.putExtra(IntentParamUtils.LOTTERYID, LotteryIDUtils.JZBF_LOTTERY_ID_INT);
                break;
            case 1:
                // 玩法介绍
                intent = new Intent(getContext(), InfoDetailActivity.class);
                intent.putExtra(IntentParamUtils.TITLE, getHelpTitle());
                intent.putExtra(IntentParamUtils.URL, getHelpUrl());
                break;
            case 2:
                // 投注查询
                if (MySharedPreferences.getInstance().getLoginStatus()) {
                    intent = new Intent(getContext(), CommonBetActivity.class);
                    intent.putExtra(IntentParamUtils.LOTTERYID, LotteryIDUtils
                            .JZRFSPF_LOTTERY_ID_INT);
                } else {
                    AppLotteryInfos.clear();
                    ViewCallBack.getInstance().countDownObjs.clear();
                    ActivityTaskManager.getInstance().closeAllActivity();
                    intent = new Intent(getContext(), LoginActivity.class);
                }
                break;
            default:
                break;
        }
        try {
            startActivity(intent);
        } catch (Exception e) {
            LogUtil.v(e.getMessage());
        }
    }

    @Override
    public void menuDismiss() {

    }

    @Override
    public void menuShow() {

    }

    private String getHelpTitle() {
        return "竞彩足球玩法介绍";
    }

    private String getHelpUrl() {
        return HelpUrlConstants.Help_JCZQ;
    }

    @Override
    public void onFilter(ArrayList<String> teams_sel) {
        curFragment.onFilter(teams_sel);
    }
}
